package com.jyserver.common.AutoCode.jybaits;

import org.mybatis.generator.api.IntrospectedTable;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;

public class ParamsGeneratorController {
    public void GeneratorController(IntrospectedTable arg1, String GeneratorController_targetPackage, String controllerhead) {
        MyJavaFormatter compilationUnit = new MyJavaFormatter(arg1, GeneratorController_targetPackage, controllerhead);
        System.err.println("生成控制类2");
        String str = compilationUnit.getallstr();
        try {
            String filename = "src/main/java/" + GeneratorController_targetPackage.replace(".", "/") + "/" + compilationUnit.getFilename();
            String[] paths = GeneratorController_targetPackage.replace(".", "/").split("/");
            String path = "src/main/java/";
            for (int i = 0; i < paths.length; i++) {
                path = path + "/" + paths[i];
                File file = new File(path);
                if (!file.exists()) {
                    file.mkdir();
                }
            }
            File file = new File(filename);
            if (file.exists()) {
                //读取文件(缓存字节流)
                BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename));

                byte[] bytes = new byte[2048];
                //接受读取的内容(n就代表的相关数据，只不过是数字的形式)
                int n = -1;
                //循环取出数据
                str = "";
                while ((n = in.read(bytes, 0, bytes.length)) != -1) {
                    str = str + new String(bytes, 0, n, "utf-8");
                }
                in.close();
                Integer k = str.indexOf("@ApiImplicitParams");
                Integer m = str.indexOf("@ResponseBody");
                if (k > 0 && m > 0) {
                    String OldParams = str.substring(k, m);
                    str = str.replace(OldParams.toString(), compilationUnit.getSwaggerString().toString()+"\n\t");
                }
            }
            FileWriter fw = new FileWriter(filename);
            fw.write(str);
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}




